package elitech.vietnam.weatherfashion.custom.dialogues;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import elitech.vietnam.weatherfashion.MainActivity;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.tabs.BaseContainerFragment;

public class TimeOutDialog {
	
	private static boolean isShowing = false;
	private Context context;
	
	public TimeOutDialog(Context context) {
		this.context = context;
	}
	
	public void show() {
		if (isShowing)
			return;
		isShowing = true;
		AlertDialog.Builder bulder = new AlertDialog.Builder(context);
		bulder.setTitle(context.getString(R.string.confirm_exit)).
		setMessage(context.getString(R.string.cannot_connect_to_server)).
		setPositiveButton(context.getString(R.string.close), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				isShowing = false;
				try {
					MainActivity activity = (MainActivity) context;
					if (activity != null) {
						BaseContainerFragment base = activity.getCurrentBase();
						if (base != null)
							base.popFragment();
					}
				} catch (ClassCastException e) {
					e.printStackTrace();
				}
				context = null;
			}
		}).create().show();
		
	}
	
	public void show(boolean finish) {
		if (isShowing)
			return;
		isShowing = true;
		AlertDialog.Builder bulder = new AlertDialog.Builder(context);
		bulder.setTitle(context.getString(R.string.confirm_exit)).
		setMessage(context.getString(R.string.cannot_connect_to_server)).
		setPositiveButton(context.getString(R.string.close), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				isShowing = false;
				try {
					((Activity)context).finish();
				} catch (ClassCastException e) {
					e.printStackTrace();
				}
				context = null;
			}
		}).create().show();
	}
}
